package com.njzxw.fs.commonutil.web;


import com.github.pagehelper.PageInfo;
import com.njzxw.fs.commonutil.bean.BaseBean;
import com.njzxw.fs.commonutil.util.StringHelper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.NoTransactionException;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.context.WebApplicationContext;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS, value = WebApplicationContext.SCOPE_REQUEST)
@Service
public class ReturnMap{

    @Autowired
    public StringHelper stringHelper;

    public org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());
    Map<String, Object> returnMap = new HashMap<String, Object>();

    /**
     * 返回状态信息
     *
     * @author eliay
     *
     */
    public enum States {
        /**
         * 访问成功 1
         */
        SUCCEE,
        /**
         * 访问失败 0
         */
        ERROR,
        /**
         * 参数异常 -9996
         */
        ERROR_PARAMS,
        /**
         * 没有访问权限 -9999
         */
        NO_AUTHORITY,
        /**
         * 没有登录，或者失效 -9998
         */
        LOGIN_LOSE,
        /**
         * 异地登录，被挤下来了 -9997
         */
        LOGIN_LOSE_CRUSH,
        /**
         * 服务器异常
         */
        ERROR_SERVER,
        /**
         * 404错误
         */
        ERROR_404,
        /**
         * 其他返回错误信息 -1
         */
        ERROR1,
        /**
         * 其他返回错误信息 -2
         */
        ERROR2,
        /**
         * 其他返回错误信息 -3
         */
        ERROR3,
        /**
         * 其他返回错误信息 -4
         */
        ERROR4,
        /**
         * 其他返回错误信息 -5
         */
        ERROR5,
        /**
         * 其他返回错误信息 -6
         */
        ERROR6,
        /**
         * 其他返回错误信息 -7
         */
        ERROR7,
        /**
         * 其他返回错误信息 -8
         */
        ERROR8,
        /**
         * 其他返回错误信息 -9
         */
        ERROR9,
        /**
         * 其他返回错误信息 -10
         */
        ERROR10,
        /**
         * 其他返回错误信息 -11
         */
        ERROR11,
        /**
         * 其他返回错误信息 -12
         */
        ERROR12,
        /**
         * 其他返回错误信息 -13
         */
        ERROR13,
        /**
         * 其他返回错误信息 -14
         */
        ERROR14,
        /**
         * 其他返回错误信息 -15
         */
        ERROR15;

        public static States getState(String states){
            switch (states) {
                case "1":
                    return SUCCEE;
                case "0":
                    return ERROR;
                case "-9999":
                    return NO_AUTHORITY;
                case "-9998":
                    return LOGIN_LOSE;
                case "-9997":
                    return LOGIN_LOSE_CRUSH;
                case  "-9996":
                    return ERROR_PARAMS;
                case "-500":
                    return ERROR_SERVER;
                case "-404":
                    return ERROR_404;
                case "-1":
                    return ERROR1;
                case "-2":
                    return ERROR2;
                case "-3":
                    return ERROR3;
                case "-4":
                    return ERROR4;
                case "-5":
                    return ERROR5;
                case "-6":
                    return ERROR6;
                case "-7":
                    return ERROR7;
                case "-8":
                    return ERROR8;
                case "-9":
                    return ERROR9;
                case "-10":
                    return ERROR10;
                case "-11":
                    return ERROR11;
                case "-12":
                    return ERROR12;
                case "-13":
                    return ERROR13;
                case "-14":
                    return ERROR14;
                case "-15":
                    return ERROR15;
                default:
                    return null;
            }
        }

        public static String getValue(States states) {
            switch (states) {
                case SUCCEE:
                    return "1";
                case ERROR:
                    return "0";
                case NO_AUTHORITY:
                    return "-9999";
                case LOGIN_LOSE:
                    return "-9998";
                case LOGIN_LOSE_CRUSH:
                    return "-9997";
                case ERROR_PARAMS :
                    return "-9996";
                case ERROR_SERVER:
                    return "-500";
                case ERROR_404:
                    return "-404";
                case ERROR1:
                    return "-1";
                case ERROR2:
                    return "-2";
                case ERROR3:
                    return "-3";
                case ERROR4:
                    return "-4";
                case ERROR5:
                    return "-5";
                case ERROR6:
                    return "-6";
                case ERROR7:
                    return "-7";
                case ERROR8:
                    return "-8";
                case ERROR9:
                    return "-9";
                case ERROR10:
                    return "-10";
                case ERROR11:
                    return "-11";
                case ERROR12:
                    return "-12";
                case ERROR13:
                    return "-13";
                case ERROR14:
                    return "-14";
                case ERROR15:
                    return "-15";
                default:
                    return "-10000";
            }
        }

    }

    public void clean() {
        this.returnMap.clear();
    }

    /**
     * 检查参数中是否包含state
     *
     * @param map
     * @return
     */
    public boolean checkStateKey(Map map) {
        if (map.containsKey("state")) {
            return true;
        }
        return false;
    }

    /**
     * 设置参数
     *
     * @param states
     */
    public void setReturnMaps(States states) {
        this.returnMap.put("state", States.getValue(states));
    }

    public void setMsgMaps(String msg){
        this.returnMap.put("msg", msg);
    }

    public void setDataMaps(Object msg){
        if(msg instanceof List){
            this.returnMap.put("data", msg);
        }else if(msg instanceof Map){
            this.returnMap.put("data", msg);
        }else{
            this.returnMap.put("data", msg);
        }

    }

    public void setPageList(List list){
        PageInfo pageInfo = new PageInfo(list);
        logger.debug("===:"+pageInfo.toString());
        this.returnMap.put("total", pageInfo.getTotal());
        this.returnMap.put("pages", pageInfo.getPages());
        this.returnMap.put("endRow", pageInfo.getEndRow());
        this.returnMap.put("isEnd", pageInfo.getEndRow() == pageInfo.getStartRow());
        this.returnMap.put("data", pageInfo.getList());
    }

    public void setPageData(List list){
        PageInfo pageInfo = new PageInfo(list);
        logger.debug("===:"+pageInfo.toString());
        this.returnMap.put("total", pageInfo.getTotal());
        this.returnMap.put("pages", pageInfo.getPages());
        this.returnMap.put("endRow", pageInfo.getEndRow());
        this.returnMap.put("isEnd", pageInfo.getEndRow() == pageInfo.getStartRow());
        this.returnMap.put("data", pageInfo.getList());
    }

    public void setReturnMap(String key, Object value) {
//        if ("state".equals(key)) {
//            logger.error("禁止返回值中手动放置参数key为state");
//            return;
//        }
        this.returnMap.put(key, value);
    }

    public ReturnMap getReturnMaps() {
        String state = stringHelper.get(this.getReturnMap(), "state");
        if (!"1".equals(state)) {
            dbRollback();
        }
        return this;
    }

    public ReturnMap getReturnMaps(States states) {
        setReturnMaps(states);
        String state = States.getValue(states);
        if (!"1".equals(state)) {
            dbRollback();
        }
        return this;
    }

    /**
     * 执行成功
     *
     * @return
     */
    public ReturnMap getSucceeReturnMaps() {
        this.setMsgMaps("操作成功");
        this.setReturnMaps(States.SUCCEE);
        return this;
    }

    /**
     * 执行成功
     *
     * @return
     */
    public ReturnMap getSucceeReturnMaps(String msg) {
        this.setMsgMaps(msg);
        this.setReturnMaps(States.SUCCEE);
        return this;
    }

    /**
     * 执行成功
     *
     * @return
     */
    public ReturnMap getSucceeReturnMaps(String key, String value) {
        this.setReturnMap(key, value);
        return getSucceeReturnMaps();
    }

    /**
     * 执行成功
     *
     * @return
     */
    public ReturnMap getSucceeReturnMaps(String key, Map map) {
        this.setReturnMap(key, JSONObject.fromObject(map));
        return getSucceeReturnMaps();
    }

    /**
     * 执行成功
     *
     * @return
     */
    public ReturnMap getSucceeReturnMaps(String key, List list) {
        this.setReturnMap(key, JSONArray.fromObject(list));
        return getSucceeReturnMaps();
    }

    /**
     * 程序出错 0
     *
     * @return
     */
    public ReturnMap getErrorReturnMaps() {
        this.setReturnMaps(States.ERROR);
        String state = StringHelper.get(this.returnMap, "state");
        if (!"1".equals(state)) {
            dbRollback();
        }
        return this;
    }

    /**
     * 程序出错 0
     *
     * @return
     */
    public ReturnMap getErrorReturnMaps(String key, String value) {
        this.returnMap.put(key, value);
        return getErrorReturnMaps();
    }

    /**
     * 程序出错 0
     *
     * @return
     */
    public ReturnMap getErrorReturnMaps(String key, Map map) {
        this.returnMap.put(key, JSONObject.fromObject(map));
        return getErrorReturnMaps();
    }

    /**
     * 程序出错 0
     *
     * @return
     */
    public ReturnMap getErrorReturnMaps(String key, List list) {
        this.returnMap.put(key, JSONArray.fromObject(list));
        return getErrorReturnMaps();
    }

    /**
     * 没有访问权限
     *
     * @return
     */
    public ReturnMap getNoAuthorityReturnMaps() {
        this.setReturnMaps(States.NO_AUTHORITY);
        return this;
    }

    /**
     * 没有访问权限
     *
     * @return
     */
    public ReturnMap getNoAuthorityReturnMaps(String key, String value) {
        this.returnMap.put(key, value);
        return getNoAuthorityReturnMaps();
    }

    /**
     * 没有访问权限
     *
     * @return
     */
    public ReturnMap getNoAuthorityReturnMaps(String key, Map map) {
        this.returnMap.put(key, JSONObject.fromObject(map));
        return getNoAuthorityReturnMaps();
    }

    /**
     * 没有访问权限
     *
     * @return
     */
    public ReturnMap getNoAuthorityReturnMaps(String key, List list) {
        this.returnMap.put(key, JSONArray.fromObject(list));
        return getNoAuthorityReturnMaps();
    }

    /**
     * 获取返回结果，可能为空值，请保证前面调用了setReturnMap方法赋值
     *
     * @return
     */
    public Map getReturnMap() {
        return this.returnMap;
    }

    /**
     * 获取当前设置的states
     * @return
     */
    public States getStates(){
        String state = StringHelper.get(this.returnMap,"state");
        return States.getState(state);
    }

    /**
     * 设置返回状态 并返回结果
     *
     * @param states
     * @return
     */
    public ReturnMap getReturnMap(States states) {
        String state = States.getValue(states);
        this.returnMap.put("state", state);
        if (!"1".equals(state)) {
            dbRollback();
        }
        return this;
    }

    /**
     * 回滚事物
     */
    private void dbRollback() {
        try {
            System.out.println("是否是新事物："+TransactionAspectSupport.currentTransactionStatus().isNewTransaction());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        } catch (NoTransactionException ex) {
        } catch (Exception ex) {
            logger.debug("事物回滚出错", ex);
        }
    }

    /**
     * 执行成功
     *
     * @return
     */
    public ReturnMap getSucceeReturnMap() {
        this.setMsgMaps("操作成功");
        return getReturnMap(States.SUCCEE);
    }

    /**
     * 执行成功
     *
     * @return
     */
    public ReturnMap getSucceeReturnMap(String key, String value) {
        this.returnMap.put(key, value);
        return getSucceeReturnMap();
    }

    /**
     * 执行成功
     *
     * @return
     */
    public ReturnMap getSucceeReturnMap(String key, Map map) {
        return getSucceeReturnMap(key, JSONObject.fromObject(map));
    }

    /**
     * 执行成功
     *
     * @return
     */
    public ReturnMap getSucceeReturnMap(String key, JSONObject json) {
        this.returnMap.put(key, json);
        return getSucceeReturnMap();
    }

    /**
     * 执行成功
     *
     * @return
     */
    public ReturnMap getSucceeReturnMap(String key, List list) {
        this.returnMap.put(key, JSONArray.fromObject(list));
        return getSucceeReturnMap();
    }

    /**
     * 程序出错
     *
     * @return
     */
    public ReturnMap getErrorReturnMap() {
        return getReturnMap(States.ERROR);
    }

    /**
     * 程序出错
     *
     * @return
     */
    public ReturnMap getErrorReturnMap(String key, String value) {
        this.returnMap.put(key, value);
        return getErrorReturnMap();
    }

    /**
     * 程序出错
     *
     * @return
     */
    public ReturnMap getErrorReturnMap(String key, Map map) {
        this.returnMap.put(key, JSONObject.fromObject(map));
        return getErrorReturnMap();
    }

    /**
     * 程序错误
     *
     * @param msg 提示消息
     *
     * @return
     */
    public ReturnMap getErrorReturnMap(String msg) {
        this.returnMap.put("msg", msg);
        return getErrorReturnMap();
    }

    /**
     * 程序出错
     *
     * @return
     */
    public ReturnMap getErrorReturnMap(String key, List list) {
        this.returnMap.put(key, JSONArray.fromObject(list));
        return getErrorReturnMap();
    }

    /**
     * 没有访问权限
     *
     * @return
     */
    public ReturnMap getNoAuthorityReturnMap() {
        return getReturnMap(States.NO_AUTHORITY);
    }

    /**
     * 没有访问权限
     *
     * @return
     */
    public ReturnMap getNoAuthorityReturnMap(String key, String value) {
        this.returnMap.put(key, value);
        return getNoAuthorityReturnMap();
    }

    /**
     * 没有访问权限
     *
     * @return
     */
    public ReturnMap getNoAuthorityReturnMap(String key, Map map) {
        this.returnMap.put(key, JSONObject.fromObject(map));
        return getNoAuthorityReturnMap();
    }

    /**
     * 没有访问权限
     *
     * @return
     */
    public ReturnMap getNoAuthorityReturnMap(String key, List list) {
        this.returnMap.put(key, JSONArray.fromObject(list));
        return getNoAuthorityReturnMap();
    }

}
